<?php

namespace App\Http\Controllers\Backend;

use App\DataTables\ChannelDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateChannelRequest;
use App\Http\Requests\UpdateChannelRequest;
use App\Repositories\ChannelRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;

class ChannelController extends AppBaseController
{
    /** @var  ChannelRepository */
    private $channelRepository;

    public function __construct(ChannelRepository $channelRepo)
    {
        $this->channelRepository = $channelRepo;
    }

    /**
     * Display a listing of the Channel.
     *
     * @param ChannelDataTable $channelDataTable
     * @return Response
     */
    public function index(ChannelDataTable $channelDataTable)
    {
        return $channelDataTable->render('backend.channels.index');
    }

    /**
     * Show the form for creating a new Channel.
     *
     * @return Response
     */
    public function create()
    {
        return view('backend.channels.create');
    }

    /**
     * Store a newly created Channel in storage.
     *
     * @param CreateChannelRequest $request
     *
     * @return Response
     */
    public function store(CreateChannelRequest $request)
    {
        $input = $request->all();

        $channel = $this->channelRepository->create($input);

        Flash::success(trans('flash.controller.save_success', ['model' => 'Channel']));

        return redirect(route('channels.index'));
    }

    /**
     * Display the specified Channel.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $channel = $this->channelRepository->findWithoutFail($id);

        if (empty($channel)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel']));

            return redirect(route('channels.index'));
        }

        return view('backend.channels.show')->with('channel', $channel);
    }

    /**
     * Show the form for editing the specified Channel.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $channel = $this->channelRepository->findWithoutFail($id);

        if (empty($channel)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel']));

            return redirect(route('channels.index'));
        }

        return view('backend.channels.edit')->with('channel', $channel);
    }

    /**
     * Update the specified Channel in storage.
     *
     * @param  int              $id
     * @param UpdateChannelRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateChannelRequest $request)
    {
        $channel = $this->channelRepository->findWithoutFail($id);

        if (empty($channel)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel']));

            return redirect(route('channels.index'));
        }

        $channel = $this->channelRepository->update($request->all(), $id);

        Flash::success(trans('flash.controller.update_success', ['model' => 'Channel']));

        return redirect(route('channels.index'));
    }

    /**
     * Remove the specified Channel from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $channel = $this->channelRepository->findWithoutFail($id);

        if (empty($channel)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel']));

            return redirect(route('channels.index'));
        }

        $this->channelRepository->delete($id);

        Flash::success(trans('flash.controller.delete_success', ['model' => 'Channel']));

        return redirect(route('channels.index'));
    }
}
